package org.test.ddd.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.test.ddd.domain.models.*;
import org.test.ddd.service.dto.LoanRecordDTO;

import java.math.BigDecimal;
import java.util.List;

/**
 * 杠杆服务
 */
@Service
public class LeverUserService {
    @Autowired
    private LeverUserRepository leverUserRepository;

    /**
     * 分页借贷记录
     * @param userId
     * @param from
     * @param pageSize
     * @return
     */
    public List<LoanRecordDTO> paginateLoanRecords(int userId, int from, int pageSize) {
        LeverUser leverUser = leverUserRepository.findById(userId);
        List<LoanRecord> loanRecords = leverUser.getMyLoanRecords().subList(from, pageSize);
        List<LoanRecordDTO> loanRecordDTOS = null;//

        return loanRecordDTOS;
    }

    /**
     * 借款
     * @param userId
     * @return
     */
    public boolean borrow(int userId, BigDecimal amount) {
        LeverUser leverUser = leverUserRepository.findById(userId);
        LeverBorrower leverBorrower = LeverBorrower.asLeverBorrower(leverUser);
        return leverBorrower.borrow(amount);
    }

    /**
     * 还款
     * @param userId
     * @param recordId
     * @param amount
     * @return
     */
    public boolean returnBorrow(int userId, int recordId, BigDecimal amount) {
        LeverUser leverUser = leverUserRepository.findById(userId);
        LeverPayer leverPayer = LeverPayer.asLeverPayer(leverUser);
        return leverPayer.returnBorrow(recordId, amount);
    }
}
